package com.ricchieri.lazymodel;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.primefaces.model.LazyScheduleModel;
import org.primefaces.model.ScheduleModel;

import com.ricchieri.common.CustomScheduleEvent;
import com.ricchieri.filter.ScheduleCharterFilter;
import com.ricchieri.model.ScheduleCharter;
import com.ricchieri.service.interf.IScheduleCharterService;

public abstract class ScheduleLazyModel extends LazyScheduleModel implements Serializable {

	private static final long serialVersionUID = -2189884066672768417L;
	private IScheduleCharterService scheduleCharterService;
	private ScheduleCharterFilter scheduleCharterFilter;
	private ScheduleModel scheduleModel;
	
	public ScheduleLazyModel(IScheduleCharterService scheduleCharterService, ScheduleCharterFilter scheduleCharterFilter, ScheduleModel scheduleModel) {
		this.scheduleCharterService = scheduleCharterService;
		this.scheduleCharterFilter = scheduleCharterFilter;
		this.scheduleModel = scheduleModel;
	}

	@Override
	public void loadEvents(Date start, Date end) {
		clear();
		
		scheduleCharterFilter.setFinish(end);
		scheduleCharterFilter.setStart(start);
		CustomScheduleEvent customEvent;
		
		List<ScheduleCharter> scheduleCharters = scheduleCharterService.getAllScheduleCharterPaginatedByFilter(scheduleCharterFilter);
		for (ScheduleCharter scheduleChar : scheduleCharters) {
			customEvent = new CustomScheduleEvent(scheduleChar);
			customEvent.setStyleClass(obtainStyle(customEvent));
			
			String valueTitle = " ".concat(String.valueOf(customEvent.getScheduleCharter().getQuantityReserved()));
			valueTitle = valueTitle.concat(" - ").concat(String.valueOf(customEvent.getScheduleCharter().getCharter().getPlaces()));
			customEvent.setTitle(valueTitle);
			addEvent(customEvent);
		}
		
		CustomScheduleEvent event;
		for (int i = 0; i < scheduleModel.getEvents().size(); i++) {		
			event = (CustomScheduleEvent) scheduleModel.getEvents().get(i);
			addEvent(event);
		}
	}

	public IScheduleCharterService getScheduleCharterService() {
		return scheduleCharterService;
	}

	public void setScheduleCharterService(
			IScheduleCharterService scheduleCharterService) {
		this.scheduleCharterService = scheduleCharterService;
	}

	public ScheduleCharterFilter getScheduleCharterFilter() {
		return scheduleCharterFilter;
	}

	public void setScheduleCharterFilter(ScheduleCharterFilter scheduleCharterFilter) {
		this.scheduleCharterFilter = scheduleCharterFilter;
	}	
	
	public abstract String obtainStyle(CustomScheduleEvent customEvent);
	
}
